package com.starbucks.opencase.tool;

import com.starbucks.opencase.entity.entAdminLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class toolAdminAccount {
    @Autowired
    private JdbcTemplate con;

    /**
     * 检测是否存在账号
     * @param phone
     * @return
     */
    public boolean exist(String phone){
        return con.queryForObject("select count(*) from admin_login where phone=?", Integer.class,phone)>0;
    }

    /**
     * 添加账号
     * @param info
     * @return
     */
    public boolean add(entAdminLogin info){
        return con.update("insert into admin_login values(?,?,?,?,?,?)",
                info.getPhone(),
                info.getName(),
                info.getCommand(),
                info.isLock()?1:0,
                info.getAuthority(),
                info.getTimestamp())>0;
    }

    /**
     * 查询口令
     * @param phone
     * @return
     */
    public String queryCommand(String phone){
        return con.queryForObject("select command from admin_login where phone=?", String.class,phone);
    }

    /**
     * 是否锁定
     * @param phone
     * @return
     */
    public boolean queryLock(String phone){
        return con.queryForObject("select `lock` from admin_login where phone=?", Integer.class,phone)==1;
    }

    /**
     * 查询管理员信息
     * @param phone
     * @return
     */
    public entAdminLogin queryInfo(String phone){
        List<entAdminLogin> qw=con.query("select * from admin_login where phone=?",new BeanPropertyRowMapper<>(entAdminLogin.class),phone);
        if(qw.isEmpty())return null;
        return qw.get(0);
    }

    /**
     * 设置登录口令
     * @param phone
     * @param command
     */
    public void setCommand(String phone,String command){
        con.update("update admin_login set command=? where phone=?",command,phone);
    }
}
